﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TenderSampleApp.Models.Profile;
using System.Web.Profile;
using System.Web.Security;
using TenderSampleApp.Code;

namespace TenderSampleApp.Controllers
{
    public class ProfileController : Controller
    {
        //
        // GET: /Profile/

        //
        //Set user profile


        [HttpGet]
        [Authorize(Roles = "USER")]
        public ActionResult FillUserProfile()
        {
            Session["userprofile"] = null;
            MembershipUser user = Membership.GetUser();
            ProfileBase profile = ProfileBase.Create(user.UserName, user.IsApproved);
            string typeOfPerson = profile.GetPropertyValue("typeOfPerson").ToString();

            if (typeOfPerson == "Фізична особа")
                return RedirectToAction("FillPhysicalUserProfile");
            else
                return RedirectToAction("FillJuridicalUserProfile");
        }

        [Authorize(Roles = "USER")]
        public ActionResult GetUserProfile()
        {
            ProfileMethod pm = new ProfileMethod();
            MembershipUser user = Membership.GetUser();
            ProfileBase profile = ProfileBase.Create(user.UserName, user.IsApproved);
            string typeOfPerson = profile.GetPropertyValue("typeOfPerson").ToString();

            if (typeOfPerson == "Фізична особа")
            {
                ViewData.Model = pm.GetPhysicalUserProfileMethod();
                return View("GetPhysicalUserProfile");
            }
            else
            {
                ViewData.Model = pm.GetJuridicalUserProfileMethod();
                return View("GetJuridicalUserProfile");
            }
        }

        #region PhysicalUserProfileAction
        [HttpGet]
        [Authorize(Roles = "USER")]
        public ActionResult FillPhysicalUserProfile()
        {
            return View();
        }

        [HttpPost]
        [Authorize(Roles = "USER")]
        public ActionResult FillPhysicalUserProfile(PhysicalUserProfileModel userprofile)
        {
            if (ModelState.IsValid)
            {
                Session["userprofile"] = userprofile;
                return RedirectToAction("FillConfirmedPhysicalUserProfile");
            }
            return View();
        }

        [HttpGet]
        [Authorize(Roles = "USER")]
        public ActionResult FillConfirmedPhysicalUserProfile()
        {
            if (Session["userprofile"] == null)
                return RedirectToAction("FillPhysicalUserProfile");
            return View();
        }

        [HttpPost]
        [Authorize(Roles = "USER")]
        public ActionResult FillConfirmedPhysicalUserProfile(PhysicalUserProfileModel userprofile)
        {
            ProfileMethod pm = new ProfileMethod();
            if (ModelState.IsValid)
            {
                PhysicalUserProfileModel firstprof = Session["userprofile"] as PhysicalUserProfileModel;
                if (pm.IsEqualsPhysicalUserProfile(userprofile, firstprof))
                {
                    pm.FillPhysicalUserProfileMethod(userprofile);
                    return RedirectToAction("GetUserProfile");
                }
                else
                {
                    Session["userprofileconfirm"] = userprofile;
                    return RedirectToAction("CheckPhysicalUserProfile");
                }
            }
            return View();
        }

        [HttpGet]
        [Authorize(Roles = "USER")]
        public ActionResult CheckPhysicalUserProfile()
        {
            PhysicalUserProfileModel firstprof = Session["userprofile"] as PhysicalUserProfileModel;
            return View(firstprof);
        }

        [HttpPost]
        [Authorize(Roles = "USER")]
        public ActionResult CheckPhysicalUserProfile(PhysicalUserProfileModel correctprof)
        {
            ProfileMethod pm = new ProfileMethod();
            pm.FillPhysicalUserProfileMethod(correctprof);
            return RedirectToAction("GetUserProfile");
        }

        #endregion
        #region JuridicalUserProfileAction

        [HttpGet]
        [Authorize(Roles = "USER")]
        public ActionResult FillJuridicalUserProfile()
        {
            return View();
        }

        [HttpPost]
        [Authorize(Roles = "USER")]
        public ActionResult FillJuridicalUserProfile(JuridicalUserProfileModel userprofile)
        {
            if (ModelState.IsValid)
            {
                Session["userprofile"] = userprofile;
                return RedirectToAction("FillConfirmedJuridicalUserProfile");
            }
            return View();
        }

        [HttpGet]
        [Authorize(Roles = "USER")]
        public ActionResult FillConfirmedJuridicalUserProfile()
        {
            if (Session["userprofile"] == null)
                return RedirectToAction("FillJuridicalUserProfile");
            return View();
        }

        [HttpPost]
        [Authorize(Roles = "USER")]
        public ActionResult FillConfirmedJuridicalUserProfile(JuridicalUserProfileModel userprofile)
        {
            ProfileMethod pm = new ProfileMethod();
            if (ModelState.IsValid)
            {
                JuridicalUserProfileModel firstprof = Session["userprofile"] as JuridicalUserProfileModel;
                if (pm.IsEqualsJuridicalUserProfile(userprofile, firstprof))
                {
                    pm.FillJuridicalUserProfileMethod(userprofile);
                    return RedirectToAction("GetUserProfile");
                }
                else
                {
                    Session["userprofileconfirm"] = userprofile;
                    return RedirectToAction("CheckJuridicalUserProfile");
                }
            }
            return View();
        }

        [HttpGet]
        [Authorize(Roles = "USER")]
        public ActionResult CheckJuridicalUserProfile()
        {
            JuridicalUserProfileModel firstprof = Session["userprofile"] as JuridicalUserProfileModel;
            return View(firstprof);
        }

        [HttpPost]
        [Authorize(Roles = "USER")]
        public ActionResult CheckJuridicalUserProfile(JuridicalUserProfileModel correctprof)
        {
            ProfileMethod pm = new ProfileMethod();
            pm.FillJuridicalUserProfileMethod(correctprof);
            return RedirectToAction("GetUserProfile");
        }

        #endregion

    

    }
}
